package fatworm.concept.datatype;

import fatworm.util.Lib;

/**
 * data type CHAR 
 */
public class FatCharArray extends fatworm.concept.DataType{
	public static FatCharArray createInstance( int maxLength ){
		return new FatCharArray( maxLength );
	}
	
	private int maxLength;

	private FatCharArray( int maxLength ){
		this.maxLength = maxLength;
	}
	public int getMaxLength(){
		return maxLength;
	}
	
	public CharArrayValue convert(SqlValue value) throws ConvertException {
		switch( value.getType().getTypeKey() ){
			case T_CHAR	 	:
			case T_VARCHAR	:
			case T_STRING	:
				String s = value.toString();
				if( s.length() <= maxLength ) return new CharArrayValue( s );
			default:
				throw new ConvertException( value , this );
		}
	}

	public int getTypeKey() {
		return T_CHAR;
	}
	public String toString() {
		return "CHAR("+maxLength+")";
	}
	
	/** the value of char (M) , the length can't be longer than maxLength */
	public class CharArrayValue extends SqlValue{
		private String value;
		private CharArrayValue( String value ){
			Lib.assertTrue( value.length() <= maxLength );
			this.value = value;
		}
		public String toString() {
			return value;
		}
	}
	
}
